<template>
  <div class="container">
    <div class="top">
      <el-input
        style="width: 200px; margin-bottom: 5px"
        v-model="search.name"
        placeholder="请输入名称"
        clearable
      ></el-input>
      <el-input
        style="width: 200px; margin-bottom: 5px"
        v-model="search.code"
        placeholder="请输入编码"
        clearable
      ></el-input>
      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
        >查询
      </el-button>
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
      <!-- <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derive()"
        >导出</el-button
      > -->
      <el-button
        class="add"
        size="small"
        type="primary"
        @click="addPermission"
        icon="el-icon-circle-plus"
        >添加仓库
      </el-button>
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
    >
      <el-table-column
        prop="house_name"
        label="仓库名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="house_code"
        label="仓库编码"
        align="center"
      ></el-table-column>
      <!-- <el-table-column
        prop="create_id"
        label="创建人ID"
        align="center"
      ></el-table-column> -->
      <el-table-column
        prop="created_at"
        label="创建时间"
        align="center"
      ></el-table-column>
      <!-- <el-table-column
        prop="update_id"
        label="编辑人ID"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="updated_at"
        label="编辑时间"
        align="center"
      ></el-table-column> -->
      <!-- <el-table-column
        prop="typeStr"
        label="状态"
        align="center"
      ></el-table-column> -->
      <!-- <el-table-column
        prop="deleted_at"
        label="删除时间"
        align="center"
      ></el-table-column> -->
      <el-table-column label="操作" width="350" align="center">
        <template slot-scope="scope">
          <el-button
            type="info"
            size="small"
            icon="el-icon-tickets"
            @click="derive(scope)"
            >导出</el-button
          >
          <!-- <el-button
            size="mini"
            icon="el-icon-info"
            type="info"
            @click="handleinfo(scope)"
            >详情</el-button
          > -->
          <el-button
            size="mini"
            icon="el-icon-edit"
            type="primary"
            @click="houseAreaEdit(scope)"
            >区域管理</el-button
          >
          <el-button
            size="mini"
            icon="el-icon-edit"
            type="primary"
            @click="handleEdit(scope)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            icon="el-icon-delete"
            type="danger"
            @click="handleDelete(scope)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>

    <warehouseDialog
      :dialog="dialog"
      :regions="regions"
      @upData="getData"
    ></warehouseDialog>
    <warehouseInfoDialog
      :particularsDialogs="particularsDialogs"
    ></warehouseInfoDialog>
  </div>
</template>
<script>
import Pagination from "@/components/pagination";
import { getToken } from "@/utils/auth";
import { WarehouseList, Warehousedestroy } from "@/api/warehouse/warehouse";
import WarehouseDialog from "@/components/warehouse/warehouseDialog";
import WarehouseInfoDialog from "@/components/warehouse/warehouseInfoDialog";
export default {
  components: {
    pagination: Pagination,
    warehouseDialog: WarehouseDialog,
    warehouseInfoDialog: WarehouseInfoDialog,
  },
  data() {
    return {
      //页面数据
      tableData: [],
      dialog: {
        centerDialogVisible: false,
        dialogType: "",
      },
      particularsDialogs: {
        centerDialogVisible: false,
        id: "",
      },
      regions: {
        name: "",
        id: "",
      },
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      search: {
        code: "",
        name: "",
      },
    };
  },

  created() {
    //页面数据
    this.getData();
    //取初始商品列表
  },
  methods: {
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        house_code: this.search.code,
        house_name: this.search.name,
      };
      WarehouseList(data)
        .then((res) => {
          if (res.code == 0) {
            // res.data.data.forEach((res) => {
            //   res.typeStr = res.is_del == 1 ? "删除" : "正常";
            // });
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },

    // 本地搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },

    //清空
    empty() {
      this.search = {
        name: "",
        code: "",
      };
    },

    derive(scope) {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let link = document.createElement("a");
          link.href =
            this.$axios.defaults.baseURL +
            "/api/Warehousearea/exportBindShelves?token=" +
            getToken() +
            "&house_code=" +
            scope.row.house_code;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出",
          });
        });
    },
    //详情
    handleinfo(scope) {
      this.particularsDialogs.centerDialogVisible = true;
      this.particularsDialogs.id = scope.row.id;
    },
    //添加人员
    addPermission() {
      this.dialog.centerDialogVisible = true;
      this.dialog.dialogType = "";
      this.regions.name = "";
      this.regions.id = "";
    },
    //区域管理
    houseAreaEdit(scope) {
      console.log(scope.row.house_code, 5555555);
      const vm = this;
      vm.$router.push({
        path: "/select/houseAreaList",
        query: { id: scope.row.house_code },
      });
    },
    //编辑
    handleEdit(scope) {
      this.dialog.centerDialogVisible = true;
      this.dialog.dialogType = "edit";
      this.regions.name = scope.row.house_name;
      this.regions.id = scope.row.id;
    },
    //删除
    handleDelete(scope) {
      this.$confirm("是否删除该条信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            id: scope.row.id,
            type: 1,
          };
          Warehousedestroy(data).then((res) => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg,
              });
            } else if (res.code == 2001) {
              this.$confirm(res.msg, {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
              })
                .then(() => {
                  let data = {
                    id: scope.row.id,
                    type: 0,
                  };
                  Warehousedestroy(data).then((res) => {
                    if (res.code == 0) {
                      this.getData();
                      this.$message({
                        type: "success",
                        message: res.msg,
                      });
                    }
                  });
                })
                .catch(() => {
                  this.$message({
                    type: "info",
                    message: "已取消删除",
                  });
                });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;

      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;

      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
};
</script>
<style scoped>
.top {
  position: relative;
  margin-bottom: 5px;
}

.btnList {
  position: relative;
  margin-bottom: 5px;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}

.pagination {
  text-align: right;
  padding: 10px;
}
</style>